﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class Frmusercx : Form
    {
        public Frmusercx()
        {
            InitializeComponent();
        }
        public Frmusercx(string flag)
        {
            InitializeComponent();
            if (flag=="del")
            {//当前进行删除查询的某条记录的操作
                this.btnDel.Visible = true;
            }
        }
        public DataSet ds = null;

        private void btnSearch_Click(object sender, EventArgs e)
        {//数据绑定
            bindDataGird();
        }

        public void bindDataGird()
        {
            string userid = this.txtUserid.Text.Trim();
            string userlevel = (string)this.combUserlever.SelectedItem;
            UserInfoData data = new UserInfoData();
            //获取用户名
            data.Userid = userid;
            data.Userlevel = userlevel;//获取用户身份
            try
            {
               DataSet ds=UserInfoOperation.getUserInfo(data);
               this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }
        //删除按钮的代码
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.txtUserid.Text.ToString() == null || "" == this.txtUserid.Text.ToString())
            {
                MessageBox.Show("请填入要删除的记录！", "提示"); return;
            }
            else
            {
                if (MessageBox.Show("确认要删除吗？", "删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string id = this.txtUserid.Text.ToString();
                    try
                    {//判断是否有该用户
                        if (UserInfoOperation.deleteUserInfo(id))
                        {//调用删除用户函数
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGird();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "错误");
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！", "错误");
                    }
                }
            }
        }

        private void Frmusercx_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            // TODO: 这行代码将数据加载到表“学生成绩管理系统同DataSet.用户表”中。您可以根据需要移动或移除它。
            //this.用户表TableAdapter.Fill(this.学生成绩管理系统同DataSet.用户表);
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 用户权限 from 用户表 group by 用户权限";
            SqlCommand cm = new SqlCommand(str,cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combUserlever.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }

        private void dataGrid1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            System.Console.WriteLine("11");
        }
    }
}